﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;

namespace Dooshion.Utility
{
    public class DateHelper
    {
        /// <summary>
        /// 获取昨天的日期
        /// </summary>
        /// <returns></returns>
        public static DateTime GetYesterDay()
        {
            return DateTime.Now.AddDays(-1);
        }

        /// <summary>
        /// 获取上月的日期
        /// </summary>
        /// <returns></returns>
        public static DateTime GetLastMonth()
        {
            return DateTime.Now.AddDays(-30);
        }

        /// <summary>
        /// 获取当前系统日期(IIS日期)
        /// </summary>
        /// <returns></returns>
        public static DateTime GetNow()
        {
            return DateTime.Now;
        }

        /// <summary>
        /// 获取当前系统日期（短日期格式）
        /// </summary>
        /// <returns></returns>
        public static string GetNowDate()
        {
            return DateTime.Now.ToShortDateString();
        }
        /// <summary>
        /// 调整日期
        /// </summary>
        /// <param name="txtDate">时间</param>
        /// <param name="type">时间类型(d表示天，m表示月，y表示年)</param>
        /// <param name="num">调整的时间值，正数表示往后调，负数表示往前调</param>
        public static void ChangeDate(TextBox txtDate, string type, int num)
        {
            if (type.Equals("d"))
            {
                if (txtDate.Text.Trim() == "")
                    txtDate.Text = DateTime.Now.AddDays(num).ToString("yyyy-MM-dd");
                else
                    txtDate.Text = DateTime.Parse(txtDate.Text.Trim()).AddDays(num).ToString("yyyy-MM-dd");
            }
            if (type.Equals("m"))
            {
                if (txtDate.Text.Trim() == "")
                    txtDate.Text = DateTime.Now.AddMonths(num).ToString("yyyy-MM-dd");
                else
                    txtDate.Text = DateTime.Parse(txtDate.Text.Trim()).AddMonths(num).ToString("yyyy-MM-dd");
            }
            if (type.Equals("y"))
            {
                if (txtDate.Text.Trim() == "")
                    txtDate.Text = DateTime.Now.AddYears(num).ToString("yyyy-MM-dd");
                else
                    txtDate.Text = DateTime.Parse(txtDate.Text.Trim()).AddYears(num).ToString("yyyy-MM-dd");
            }
        }
        /// <summary>
        /// 调整日期
        /// </summary>
        /// <param name="txtStart">开始时间</param>
        /// <param name="txtEnd">结束时间</param>
        /// <param name="type">时间类型(d表示天，m表示月，y表示年)</param>
        /// <param name="num">调整的时间值，正数表示往后调，负数表示往前调</param>
        public static void ChangeDate(TextBox txtStart, TextBox txtEnd, string type, int num)
        {
            if (type.Equals("d"))
            {
                if (txtStart.Text.Trim() == "")
                    txtStart.Text = DateTime.Now.AddDays(num).ToString("yyyy-MM-dd");
                else
                    txtStart.Text = DateTime.Parse(txtStart.Text.Trim()).AddDays(num).ToString("yyyy-MM-dd");

                if (txtEnd.Text.Trim() == "")
                    txtEnd.Text = DateTime.Now.AddDays(num).ToString("yyyy-MM-dd");
                else
                    txtEnd.Text = DateTime.Parse(txtEnd.Text.Trim()).AddDays(num).ToString("yyyy-MM-dd");
            }
            if (type.Equals("m"))
            {
                if (txtStart.Text.Trim() == "")
                    txtStart.Text = DateTime.Now.AddMonths(num).ToString("yyyy-MM-dd");
                else
                    txtStart.Text = DateTime.Parse(txtStart.Text.Trim()).AddMonths(num).ToString("yyyy-MM-dd");

                if (txtEnd.Text.Trim() == "")
                    txtEnd.Text = DateTime.Now.AddMonths(num).ToString("yyyy-MM-dd");
                else
                    txtEnd.Text = DateTime.Parse(txtEnd.Text.Trim()).AddMonths(num).ToString("yyyy-MM-dd");
            }
            if (type.Equals("y"))
            {
                if (txtStart.Text.Trim() == "")
                    txtStart.Text = DateTime.Now.AddYears(num).ToString("yyyy-MM-dd");
                else
                    txtStart.Text = DateTime.Parse(txtStart.Text.Trim()).AddYears(num).ToString("yyyy-MM-dd");

                if (txtEnd.Text.Trim() == "")
                    txtEnd.Text = DateTime.Now.AddYears(num).ToString("yyyy-MM-dd");
                else
                    txtEnd.Text = DateTime.Parse(txtEnd.Text.Trim()).AddYears(num).ToString("yyyy-MM-dd");
            }
        }
    }
}
